package com.iRomantic.controller.app;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.iRomantic.common.AppJsonResponse;
import com.iRomantic.common.BaseController;
import com.iRomantic.config.Constant;
import com.iRomantic.domain.entity.Version;

public class VersionAPPController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(VersionAPPController.class);
	
	/**
	 * 获取版本更新
	 */
	public void getNewVersion(){
		AppJsonResponse jsonResult = this.createAppJsonResponse();
		Version version = this.getModel(Version.class);
		version = version.findLastVrsionByType(version.getInt("type"));
		if(version!=null){
			jsonResult.success("成功");
			version.setProjectPath(version);
			jsonResult.setResult(version);
		}else{
			jsonResult.error("暂无新版本");
		}
		renderJson(jsonResult);
	}
	
	public void download(){
		HttpServletRequest request = this.getRequest();
		String userAgent = request.getHeader("User-Agent").toLowerCase();
		logger.info("userAgent:"+userAgent);
		Version version = null;
		if(userAgent.contains("iphone") || userAgent.contains("ipad") || userAgent.contains("ipod")){
			logger.info("iphone");
			version = Version.DAO.findLastVrsionByType(Version.TYPE_IOS);
		}else if(userAgent.contains("android") || userAgent.contains("linux")){
			logger.info("android");
			version = Version.DAO.findLastVrsionByType(Version.TYPE_ANDROID);
		}else if(userAgent.contains("windows phone") || userAgent.contains("blackberry")){
			logger.info("windows phone");
			version = Version.DAO.findLastVrsionByType(Version.TYPE_WP);
		}
		
		if(version!=null){
			redirect(Constant.OSS_PATH+Constant.VERSION_PACKAGE+version.getStr("url"));
		}else{
			renderWebJsp("download.jsp");
		}
	}
	
	/**
	 * android版本下载
	 */
	public void android(){
		Version version = Version.DAO.findLastVrsionByType(Version.TYPE_ANDROID);
		if(version!=null){
			redirect(Constant.OSS_PATH+Constant.VERSION_PACKAGE+version.getStr("url"));
		}else{
			renderWebJsp("error/404.jsp");
		}
	}
	
	/**
	 * IOS版本下载
	 */
	public void ios(){
		Version version = Version.DAO.findLastVrsionByType(Version.TYPE_IOS);
		if(version!=null){
			redirect(Constant.OSS_PATH+Constant.VERSION_PACKAGE+version.getStr("url"));
		}else{
			renderWebJsp("error/404.jsp");
		}
	}

}
